package org.space.galaxyCabinet.config;

import com.influxdb.client.InfluxDBClient;
import com.influxdb.client.InfluxDBClientFactory;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;

import java.util.Map;

@Data
@Component
@ConfigurationProperties(prefix = "spring.influx")
public class InfluxDBConfig {

    // 从配置文件中读取 InfluxDB 的连接信息
    private String url;

    private String token;

    private String org;

    private Map<String, String> buckets;

    @Bean
    public InfluxDBClient influxDBClient() {
        return InfluxDBClientFactory.create(url, token.toCharArray(), org, "deviceLog");
    }

    // 使用 InfluxDBClientFactory 创建客户端实例
    @Bean("deviceStatusClient")
    public InfluxDBClient deviceStatusClient() {
        String bucketName = buckets.get("status");
        return InfluxDBClientFactory.create(url, token.toCharArray(), org, bucketName);
    }
    @Primary
    @Bean("deviceLogClient")
    public InfluxDBClient deviceLogClient() {
        String bucketName = buckets.get("log");
        return InfluxDBClientFactory.create(url, token.toCharArray(), org, bucketName);
    }
}
